//
//  main.swift
//  C005_test_closure_reference
//
//  Created by yao_yu on 15/5/19.
//  Copyright (c) 2015年 yao_yu. All rights reserved.
//

import Foundation

class TestClass1 {
    func doTest() {
        
    }
    deinit {
        print("\(self).销毁")
    }
}

var globalC1:TestClass1! = TestClass1()

class TestClass2 {
    func callGlobalVariable(){
        globalC1.doTest()
    }
}

func test(){
    var t1:TestClass1?
    t1 = TestClass1()
    t1 = nil
    t1?.doTest()
    TestClass2()
    globalC1 = nil
}


test()


func test2(){
    var b = 0
    
    var a11: [Int]!
    
    a11 = nil
    
    if a11 != nil {
    for _ in a11 {
        ++b
    }
    }
    
    print(b)

}

test2()



extension Array {
    
    /**
    Iterates on each element of the array.
    
    :param: call Function to call for each element
    */
    func each (call: (Element) -> ()) {
        
        for item in self {
            call(item)
        }
        
    }
    
    /**
    Iterates on each element of the array with its index.
    
    :param: call Function to call for each element
    */
    func each (call: (Int, Element) -> ()) {
        
        for (index, item) in enumerate() {
            call(index, item)
        }
        
    }
}

var a:[Int]!

a = [1,2,3,4,5]
var index = 0
var sum = 0
a?.each { (item:Int) -> Void  in
    index++
    sum += item
}

print(sum)